package com.qixin.part.controller;


import com.alibaba.fastjson.JSONObject;
import com.qixin.api.bean.*;
import com.qixin.api.service.ComPeopleService;
import com.qixin.api.service.ComPtUserService;
import com.qixin.api.service.PtMoneyInfoService;
import com.qixin.part.bean.PtUser;
import com.qixin.part.service.PtUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.*;

import javax.net.ssl.SSLEngine;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 薛向毅
 * @since 2018-09-28
 */
@Controller
@RequestMapping("/comPeople")
public class ComPeopleController {
    Logger logger = LoggerFactory.getLogger(ComPeopleController.class);
    @Autowired
    private ComPeopleService comPeopleService;

    @Autowired
    private ComPtUserService comPtUserService;

    @Autowired
    PtMoneyInfoService ptMoneyInfoService;


     /**
     * 查询企业树
     *
     */
    @RequestMapping("/findPtZtree")
    @ResponseBody
    public List<ZtreeBean> findPtZtree(  HttpSession session) {
     ComPtUser comPtUser=(ComPtUser)session.getAttribute("user");
        return comPeopleService.findPtZtree(comPtUser.getPtname(),comPtUser.getId());
    }
     /**
     * 查询用户表格
     *
     */
    @RequestMapping("/PeopleList")
    @ResponseBody
    public ResultPage<ComPeople> PeopleList(  VoPage voPage,HttpSession session,Long ptId,String peopleName) {

     ComPtUser comPtUser=(ComPtUser)session.getAttribute("user");

        return comPeopleService.PeopleList(voPage,comPtUser.getId(),ptId,peopleName);
    }
     /**
     * 添加用户
     *
     */
    @RequestMapping("/addpeople")
    @ResponseBody
    public String addpeople( String param,Long ptId) {
        ComPeople comPeople=  JSONObject.parseObject(param,ComPeople.class);
        return comPeopleService.addPeople(comPeople,ptId);
    }
     /**
     * 添加子集企业跳转
     *
     */
    @RequestMapping("/addPt.html")
    public String addPt( Long checkedId, Model model) {
        model.addAttribute("ptId",checkedId);
        return "/company/alert/Pt_add";
    }

     /**
     * 添加子集企业
     */
    @RequestMapping("/addPt")
    @ResponseBody
    public String addPt( String param ) {
        ComPtUser comPtUser= JSONObject.parseObject(param,ComPtUser.class);

        Map map=new HashMap<>();
        map.put("call_phone",comPtUser.getCallPhone());
        if(comPtUserService.selectByMap(map).size()>0){
            return "该企业已存在";
        };

        comPtUser.setUserName(comPtUser.getCallPhone());
        comPtUser.setCreateTime(new Date().getTime());
        comPtUser.setFlag("2");
        if(comPtUserService.insert(comPtUser)){
            return "添加成功";
        };
        return  "添加失败";
    }
     /**
     * 添加子集企业
     */
    @RequestMapping("/delPeople")
    @ResponseBody
    public String delPeople( Long id ) {
        ComPeople comPeople= new ComPeople();
        comPeople.setUserId(0L);
        comPeople.setId(id);
        if(comPeopleService.updateById(comPeople)){
            return "解除关联成功";
        }else{
            return  "解除关联失败";
        }
    }


    /**
     * 跳转主页面
     *
     * @return
     */
    @RequestMapping("/main.html")
    public String user1() {
        return "company/People";
    }

    /**
     * 跳转添加页面
     *
     * @return
     */
    @RequestMapping("/people_Add")
    public String people_add(Long checkedId, Model model) {
          model.addAttribute("ptId",checkedId);
        return "/company/people_Add";
    }

    /**
     * 跳转充值记录页面
     * @return
     */
    @RequestMapping("/Bill.html")
    public String Bill() {
        return "company/Bill";
    }


}






















